package com.tan.androidtest.base;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import com.tan.androidtest.R;
import com.tan.androidtest.util.NetUtil;
import com.tan.androidtest.util.StatusBarCompat;
import com.tan.androidtest.util.ToastUtil;

import butterknife.ButterKnife;

/**
 * activity的基类
 */

public abstract class BaseActivity extends AppCompatActivity {

    public TextView tv_bar_left;
    public TextView tv_bar_title;
    public TextView tv_bar_right;

    private BroadcastReceiver networkReceiver;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(initLayoutId());
        ButterKnife.bind(this);
        initBundle();
        init();
        initTitle(tv_bar_left, tv_bar_title, tv_bar_right);
        initView();
        initData();
    }

    /**
     * 初始化title相关事件
     * <p>初始化网络检查
     * <p>初始化状态栏颜色变更
     */
    private void init() {
        tv_bar_left = findViewById(R.id.tv_bar_left);
        tv_bar_title = findViewById(R.id.tv_bar_title);
        tv_bar_right = findViewById(R.id.tv_bar_right);
        tv_bar_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        //修改状态栏颜色,getStatusBarColor()可自定义颜色
        StatusBarCompat.compat(this, getStatusBarColor(), false);
        //注册网络变化广播接收器
        networkReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if (!NetUtil.isNetworkAvailable(context)) {
                    ToastUtil.showToast(getApplicationContext(), "网络已断开,请检查网络!");
                }
            }
        };

        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        registerReceiver(networkReceiver, intentFilter);
    }


    /**
     * 初始化布局id
     */
    public abstract int initLayoutId();

    /**
     * 获取传过来的数据
     */
    public void initBundle() {

    }

    /**
     * 初始化title
     */
    public abstract void initTitle(TextView tv_bar_left, TextView tv_bar_title, TextView tv_bar_right);

    /**
     * 初始化view
     */
    public abstract void initView();

    /**
     * 初始化数据
     * <p>请求网络加载数据等</p>
     */
    public abstract void initData();

    /**
     * 子类可重写该方法设置状态栏背景色
     *
     * @return
     */
    public int getStatusBarColor() {
        return ContextCompat.getColor(this, R.color.black_161616);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (networkReceiver != null) {
            unregisterReceiver(networkReceiver);
        }
    }
}
